package com.lijinjiang.reflect06_framework;

import java.io.FileOutputStream;
import java.io.PrintStream;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
 * @ClassName MybatisUtil
 * @Description TODO
 * @Author Li
 * @Date 2022/9/20 22:38
 * @ModifyDate 2022/9/20 22:38
 * @Version 1.0
 */
public class MybatisUtil {
    /**
     * 保存任意类型的对象
     */
    public static void save(Object obj) {
        try {
            PrintStream ps = new PrintStream(new FileOutputStream(
                    "P02_Reflect/src/com/lijinjiang/reflect06_framework/data.txt", true));
            // 1.获取class对象
            Class c = obj.getClass(); // c.getSimpleName() 获取当前类名  c.getName() 获取全限名：包名+类名
            String simpleName = c.getSimpleName();
            ps.println("===============" + simpleName + "===============");
            // 2.获取全部成员变量
            Field[] fields = c.getDeclaredFields();
            // 3.获取成员变量的值
            for (Field field : fields) {
                field.setAccessible(true);
                String name = field.getName();
                String value = field.get(obj) + "";
                ps.println(name + "=" + value);
            }
            ps.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
